package org.example.leetcode;

import java.util.List;

/**
 * @version 1.0.0
 * @author: lynn
 * @description: 合并两个升序链表
 * @date: 2021/7/26 22:23
 */
public class LC21 {
    public static void main(String[] args) {

    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        if(l2==null) return l1;
        ListNode head= (l1.val<l2.val)?l1:l2;
        ListNode tail=head;
        l1=head==l1?l1.next:l1;
        l2=head==l2?l2.next:l2;
        while (l1!=null&&l2!=null){
            if (l1.val<l2.val){
                tail.next=l1;
                l1=l1.next;
            }else {
                tail.next=l2;
                l2=l2.next;
            }
            tail=tail.next;
        }
        if (l1!=null){
            tail.next=l1;
        }
        if (l2!=null){
            tail.next=l2;
        }
        return head;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
